package com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.repository.impl;

import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.entity.EnterpriseLossComment;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.entity.EnterpriseLossCurrentAssets;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.entity.EnterpriseLossFixedAssets;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.repository.EnterpriseLossRepository;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.vo.ResultVo;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/02/15
 * @Description: 企业地震损失报表
 */
@Repository
public class EnterpriseLossRepositoryImpl implements EnterpriseLossRepository {
    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private static final String FIXED = "xczh_loss_enterprise_fixed_assets"; // 企业地震损失报表_固定资产部分
    private static final String CURRENT = "xczh_loss_enterprise_current_assets"; // 企业地震损失报表_流动资产部分
    private static final String COMMENT = "xczh_loss_enterprise_comment"; // 企业地震损失报表_备注部分

    @Override
    public void deleteByXlrmId(String xlrmId) {
        String sql = "delete from  "+FIXED+"  where xlrm_id= '"+xlrmId+"'" ;
        String sql2 = "delete from  "+CURRENT+"  where xlrm_id= '"+xlrmId+"'" ;
        String sql3 = "delete from  "+COMMENT+"  where xlrm_id= '"+xlrmId+"'" ;
        jdbcTemplate.update(sql);
        jdbcTemplate.update(sql2);
        jdbcTemplate.update(sql3);
    }

    @Override
    public void batchFixedSave(List<EnterpriseLossFixedAssets> list) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(list.toArray());
        String sql = "INSERT INTO "+FIXED +
                " ( id,xlrm_id,fill_form_user,tel,fill_form_time,province,city,county,township," +
                "enterprise_name,subordination,main_product,annual_production_value,fixed_assets," +
                "current_assets,asset_type,asset_name,original_value,purchase_time,damage_condition_desc," +
                "damage_grade,loss,recovery_mode,recovery_time,stop_production_loss,restore_money,sequence)" +
                "values (:id,:xlrmId,:fillFormUser,:tel,:fillFormTime,:province,:city,:county,:township," +
                ":enterpriseName,:subordination,:mainProduct,:annualProductionValue,:fixedAssets," +
                ":currentAssets,:assetType,:assetName,:originalValue,:purchaseTime,:damageConditionDesc," +
                ":damageGrade,:loss,:recoveryMode,:recoveryTime,:stopProductionLoss,:restoreMoney,:sequence)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

    @Override
    public void batchCurrentSave(List<EnterpriseLossCurrentAssets> list) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(list.toArray());
        String sql = "INSERT INTO "+CURRENT +
                " ( id,xlrm_id,asset_type,asset_name,original_value,total," +
                "damage_condition_desc,loss,sequence,fill_form_user,tel,fill_form_time,province,city,county,township," +
                "enterprise_name,subordination,main_product,annual_production_value,fixed_assets,current_assets)" +
                "values (:id,:xlrmId,:assetType,:assetName,:originalValue,:total," +
                ":damageConditionDesc,:loss,:sequence,:fillFormUser,:tel,:fillFormTime,:province,:city,:county,:township," +
                ":enterpriseName,:subordination,:mainProduct,:annualProductionValue,:fixedAssets,:currentAssets)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

    @Override
    public void saveComment(EnterpriseLossComment comment) {
        String sql ="INSERT INTO "+ COMMENT + "( id,xlrm_id,comment) values (:id,:xlrmId,:comment)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(comment);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public List<ResultVo> getFixedAssetsTotalByXsemId(String xsemId,String reportType) {
        String sql = "select t2.xsem_id,t1.province,t1.city,t1.county, (CAST(t1.loss AS DECIMAL(15,2))) as loss " +
                "from xczh_loss_enterprise_fixed_assets t1 left join xczh_loss_report_manage t2 on t1.xlrm_id=t2.id " +
                "where t2.xsem_id = '"+xsemId+"' and t2.report_type = '"+reportType+"' and t1.asset_type='合计'";
        List<ResultVo> list = namedParameterJdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ResultVo.class));
        return list;
    }

    @Override
    public List<ResultVo> getCurrentTotalByXsemId(String xsemId,String reportType) {
        String sql = "select t2.xsem_id,t1.province,t1.city,t1.county,(CAST(t1.loss AS DECIMAL(15,2))) as loss " +
                "from xczh_loss_enterprise_current_assets t1 left join xczh_loss_report_manage t2 on t1.xlrm_id=t2.id " +
                "where t2.xsem_id = '"+xsemId+"' and t2.report_type = '"+reportType+"' and t1.asset_type='合计'";
        List<ResultVo> list = namedParameterJdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ResultVo.class));
        return list;
    }
}
